#include "sys.h"
#include "Circle_queue.h"
#include "usart.h"
#include "string.h"
#include "receive.h"
#include "delay.h"

extern struct circle_queue rx_buffer;
extern unsigned char *ptem;
extern unsigned char tem[4100];
extern char step,t2;
extern int length,t;
extern char url[],wifi[],password[];
extern char flag_stop,flag_wifi;

unsigned char Wait_Ack(void)
{
	if(available(&rx_buffer) ==0 )
	{
		t++;
		if(t == 32767)
		{
			t=0;
			t2++;
			if(t2==100)
			{
				t2=0;
				return 1;
			}
		}
	}
	return 0;
}

char try_quit(void)
{
	if( !GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_4) )
	{
		delay_ms(100);
		if( !GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_4) )
		{
			while(!GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_4));
			return 1;
		}
	}
	return 0;
}
void wifi_connect(void)
{
	ptem=tem;
	tem[0] = 0;
	tem[1] = 1;
	while(available(&rx_buffer)>0)
	{
		*ptem=queue_read(&rx_buffer);
	}
	printf("AT+CWMODE=1\r\n");
	while(flag_wifi==0)
	{
		if(try_quit())
			return;
		while(available(&rx_buffer)>0)
		{
		  __ASM("NOP");
		  *ptem=queue_read(&rx_buffer);
			if(*ptem == 'O')
				 ptem++;
			if((ptem != &tem[0]) && (*ptem=='K'))
				 flag_wifi = 1;	
			t2 = 0;
		}
		if( Wait_Ack() )
				printf("AT+CWMODE=1\r\n");
	}
	
	ptem=tem;
	tem[0] = 0;
	tem[1] = 1;
	while(available(&rx_buffer)>0)
	{
		*ptem=queue_read(&rx_buffer);
	}
	flag_wifi = 0;
	printf("AT+CWJAP=\"%s\",\"%s\"\r\n",wifi,password);
	while(flag_wifi==0)
	{
		if(try_quit())
			return;
		
		while(available(&rx_buffer)>0)
		{
		  __ASM("NOP");
		  *ptem=queue_read(&rx_buffer);
			if(*ptem == 'O')
				 ptem++;
			 if((ptem != &tem[0]) && (*ptem=='K'))
				 flag_wifi=1;					
			t2 = 0;
		}
		if( Wait_Ack() )
				printf("AT+CWJAP=\"%s\",\"%s\"\r\n",wifi,password);
	}
}

unsigned char Setup_TCP(void)
{
	unsigned flag = 0;
	
	while(available(&rx_buffer)>0)
	{
		*ptem=queue_read(&rx_buffer);
	}
	printf("AT+CIPSTART=\"TCP\",\"120.26.123.202\",8080\r\n");
	ptem=tem;
	tem[0] = 0;
	tem[1] = 1;
	while(flag==0)
	{
		if(try_quit())
			return 0;
		while(available(&rx_buffer)>0)
		{
		  __ASM("NOP");
		  *ptem=queue_read(&rx_buffer);
			if(*ptem == 'O')
				 ptem++;
			 if((ptem != &tem[0]) && (*ptem=='K'))
				 flag=1;		
			 t2 = 0;
		}
		if( Wait_Ack() )
			printf("AT+CIPSTART=\"TCP\",\"120.26.123.202\",8080\r\n");
	}
	return 1;
}

unsigned char CipSend(void)
{
	unsigned flag = 0;
	
	while(available(&rx_buffer)>0)
	{
		*ptem=queue_read(&rx_buffer);
	}
	//length=strlen(url)+8;
	length=strlen(url);
	printf("AT+CIPSEND=");
	printf("%d\r\n",length);
  ptem=&tem[0];
	tem[0] = 0;
	tem[1] = 1;
	while(flag==0)
	{
		 if(try_quit())
			break;
		 while(available(&rx_buffer)>0)
		 {
			 *ptem=queue_read(&rx_buffer);
			 if(*ptem == 'O')
				 ptem++;
			 if((ptem != &tem[0]) && (*ptem=='K'))
				 flag=1;	
			 t2=0;					 
		 }
			if( Wait_Ack() )
			{
				printf("AT+CIPSEND=");
				printf("%d\r\n",length);
			}
	}
	return 1;
}

unsigned char Get_Data(void)
{	
	printf(url);
	ptem=&tem[0];
	while(flag_stop == 0)
	{
		if(try_quit())
			break;
		 __ASM("NOP");
		receive_handle();
	  if( Wait_Ack() )
		{
			if( CipSend() )
				printf(url);
		}	
	}
	return 1;
}
